home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50a Issue 142 (CD142a) (August 1998).iso / trial / demon / TURNPIKE.1 / CLASSES.ZIP / sun / AWT / IMAGE / GifImageDecoder.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-04-14  |  4.0 KB  |  206 lines

  1. package sun.awt.image;
  2.  
  3. import java.awt.image.ImageConsumer;
  4. import java.awt.image.IndexColorModel;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.Hashtable;
  8.  
  9. public class GifImageDecoder extends ImageDecoder {
  10.    private final boolean verbose = false;
  11.    private static final int IMAGESEP = 44;
  12.    private static final int EXBLOCK = 33;
  13.    private static final int EX_GRAPHICS_CONTROL = 249;
  14.    private static final int EX_COMMENT = 254;
  15.    private static final int EX_APPLICATION = 255;
  16.    private static final int TERMINATOR = 59;
  17.    private static final int INTERLACEMASK = 64;
  18.    private static final int COLORMAPMASK = 128;
  19.    PixelStore8 store;
  20.    int num_colors;
  21.    byte[] colormap;
  22.    IndexColorModel model;
  23.    Hashtable props = new Hashtable();
  24.    private static final int normalflags = 30;
  25.    private static final int interlaceflags = 29;
  26.  
  27.    public GifImageDecoder(InputStreamImageSource var1, InputStream var2) {
  28.       super(var1, var2);
  29.    }
  30.  
  31.    public synchronized boolean catchupConsumer(InputStreamImageSource var1, ImageConsumer var2) {
  32.       return this.store == null || this.store.replay(var1, var2);
  33.    }
  34.  
  35.    public synchronized void makeStore(int var1, int var2) {
  36.       this.store = new PixelStore8(var1, var2);
  37.    }
  38.  
  39.    private static void error(String var0) throws ImageFormatException {
  40.       throw new ImageFormatException(var0);
  41.    }
  42.  
  43.    boolean readBytes(byte[] var1, int var2, int var3) {
  44.       while(var3 > 0) {
  45.          int var4;
  46.          try {
  47.             var4 = super.input.read(var1, var2, var3);
  48.          } catch (IOException var5) {
  49.             var4 = -1;
  50.          }
  51.  
  52.          if (var4 < 0) {
  53.             return false;
  54.          }
  55.  
  56.          var2 += var4;
  57.          var3 -= var4;
  58.       }
  59.  
  60.       return true;
  61.    }
  62.  
  63.    public void produceImage() throws IOException, ImageFormatException {
  64.       int var1 = -1;
  65.  
  66.       try {
  67.          this.readHeader();
  68.  
  69.          label248:
  70.          while(true) {
  71.             switch (super.input.read()) {
  72.                case -1:
  73.                   return;
  74.                case 33:
  75.                   int var4;
  76.                   String var21;
  77.                   switch (var4 = super.input.read()) {
  78.                      case 249:
  79.                         byte[] var5 = new byte[6];
  80.                         if (!this.readBytes(var5, 0, 6)) {
  81.                            return;
  82.                         }
  83.  
  84.                         if (var5[0] != 4 || var5[5] != 0) {
  85.                            return;
  86.                         }
  87.  
  88.                         var1 = var5[4] & 255;
  89.                         continue;
  90.                      case 254:
  91.                      case 255:
  92.                      default:
  93.                         var21 = "";
  94.                   }
  95.  
  96.                   while(true) {
  97.                      int var6 = super.input.read();
  98.                      if (var6 == 0) {
  99.                         if (var4 == 254) {
  100.                            this.props.put("comment", var21);
  101.                         }
  102.                         continue label248;
  103.                      }
  104.  
  105.                      byte[] var7 = new byte[var6];
  106.                      if (!this.readBytes(var7, 0, var6)) {
  107.                         return;
  108.                      }
  109.  
  110.                      if (var4 == 254) {
  111.                         var21 = var21 + new String(var7, 0);
  112.                      }
  113.                   }
  114.                case 44:
  115.                   try {
  116.                      this.model = new IndexColorModel(8, this.num_colors, this.colormap, 0, false, var1);
  117.                      this.colormap = null;
  118.                      if (this.readImage()) {
  119.                         this.store.imageComplete();
  120.                         if (this.store.getBitState() != 2) {
  121.                            super.source.setPixelStore(this.store);
  122.                         }
  123.  
  124.                         super.source.imageComplete(3);
  125.                         return;
  126.                      }
  127.                   } catch (ArrayIndexOutOfBoundsException var19) {
  128.                   }
  129.  
  130.                   return;
  131.                case 59:
  132.                   return;
  133.                default:
  134.                   return;
  135.             }
  136.          }
  137.       } finally {
  138.          try {
  139.             super.input.close();
  140.          } catch (IOException var18) {
  141.          }
  142.  
  143.       }
  144.    }
  145.  
  146.    private void readHeader() throws IOException, ImageFormatException {
  147.       byte[] var1 = new byte[13];
  148.       if (!this.readBytes(var1, 0, 13)) {
  149.          throw new IOException();
  150.       } else if (var1[0] == 71 && var1[1] == 73 && var1[2] == 70) {
  151.          int var2 = var1[10] & 255;
  152.          if ((var2 & 128) == 0) {
  153.             throw new ImageFormatException("no global colormap in GIF file.");
  154.          } else {
  155.             this.num_colors = 1 << (var2 & 7) + 1;
  156.             if (var1[12] != 0) {
  157.                this.props.put("aspectratio", "" + (double)((var1[12] & 255) + 15) / (double)64.0F);
  158.             }
  159.  
  160.             this.colormap = new byte[this.num_colors * 3];
  161.             if (!this.readBytes(this.colormap, 0, this.num_colors * 3)) {
  162.                throw new IOException();
  163.             }
  164.          }
  165.       } else {
  166.          throw new ImageFormatException("not a GIF file.");
  167.       }
  168.    }
  169.  
  170.    private native boolean parseImage(int var1, int var2, boolean var3, int var4, byte[] var5, byte[] var6);
  171.  
  172.    private int sendPixels(int var1, int var2, byte[] var3) {
  173.       int var4 = super.source.setPixels(0, var1, var2, 1, this.model, var3, 0, var2);
  174.       if (this.store.setPixels(0, var1, var2, 1, var3, 0, var2)) {
  175.          ++var4;
  176.       }
  177.  
  178.       return var4;
  179.    }
  180.  
  181.    private boolean readImage() throws IOException {
  182.       long var1 = 0L;
  183.       byte[] var3 = new byte[259];
  184.       if (!this.readBytes(var3, 0, 10)) {
  185.          throw new IOException();
  186.       } else {
  187.          int var4 = var3[4] & 255 | var3[5] << 8;
  188.          int var5 = var3[6] & 255 | var3[7] << 8;
  189.          boolean var6 = (var3[8] & 64) != 0;
  190.          int var7 = var3[9] & 255;
  191.          super.source.setDimensions(var4, var5);
  192.          this.makeStore(var4, var5);
  193.          super.source.setProperties(this.props);
  194.          this.store.setProperties(this.props);
  195.          super.source.setColorModel(this.model);
  196.          this.store.setColorModel(this.model);
  197.          int var8 = var6 ? 29 : 30;
  198.          super.source.setHints(var8);
  199.          this.store.setHints(var8);
  200.          byte[] var9 = new byte[var4];
  201.          boolean var10 = this.parseImage(var4, var5, var6, var7, var3, var9);
  202.          return var10;
  203.       }
  204.    }
  205. }
  206.